package app.android.kdictionary.Util;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class HttpRequester {
	private URL url = null;
	private URLConnection connection = null;
	private HttpURLConnection hURLc = null;
	private InputStreamReader isr = null;
	private BufferedReader br = null;
	private OutputStream out = null;

	
	public void close() throws IOException {
		if(hURLc != null) {
			hURLc.disconnect();
		}
		if(br != null) {
			br.close();
		}
		if(isr != null) {
			isr.close();
		}
		if(out != null) {
			out.close();
		}
	}
	
	public String request(String urlString, String method) throws IOException, HttpConnectException {
		
		url = new URL(urlString);
		connection = url.openConnection();
		hURLc = (HttpURLConnection)connection;
		
		hURLc.setRequestMethod(method);
		hURLc.setDoOutput(true);
		hURLc.setDoInput(true);
		hURLc.setUseCaches(false);
		hURLc.setDefaultUseCaches(false);

		
		isr = new InputStreamReader(hURLc.getInputStream(), "UTF-8");
		br = new BufferedReader(isr);
		
		if(hURLc.getResponseCode() != HttpURLConnection.HTTP_OK) {
			throw new HttpConnectException(hURLc.getResponseCode());
		}

		StringBuffer sb = new StringBuffer();
		String tmp;
		
		while((tmp = br.readLine()) != null) {
			sb.append(tmp + "\n");
		}
		
		close();
		
		return sb.toString();
	}
	
	public String request(String urlString, String method, String param) throws IOException, HttpConnectException {
		if(method.equals("GET")) {
			url = new URL(urlString+"?"+param);
			connection = url.openConnection();
			hURLc = (HttpURLConnection)connection;
			
			hURLc.setRequestMethod(method);
			hURLc.setDoOutput(true);
			hURLc.setDoInput(true);
			hURLc.setUseCaches(false);
			hURLc.setDefaultUseCaches(false);
			hURLc.setConnectTimeout(10000);
			
			isr = new InputStreamReader(hURLc.getInputStream(), "UTF-8");
			br = new BufferedReader(isr);
			
			if(hURLc.getResponseCode() != HttpURLConnection.HTTP_OK) {
				throw new HttpConnectException(hURLc.getResponseCode());
			}
			
			StringBuffer sb = new StringBuffer();
			String tmp;

			while((tmp = br.readLine()) != null) {
				sb.append(tmp + "\n");
			}

			close();
			
			return sb.toString();
			
		} else {
			url = new URL(urlString);
			connection = url.openConnection();
			hURLc = (HttpURLConnection)connection;
			
			hURLc.setRequestMethod(method);
			hURLc.setDoOutput(true);
			hURLc.setDoInput(true);
			hURLc.setUseCaches(false);
			hURLc.setDefaultUseCaches(false);

			out = hURLc.getOutputStream();
			out.write(param.getBytes("UTF-8"));
			out.flush();

			isr = new InputStreamReader(hURLc.getInputStream(), "UTF-8");
			br = new BufferedReader(isr);
			
			if(hURLc.getResponseCode() != HttpURLConnection.HTTP_OK) {
				throw new HttpConnectException(hURLc.getResponseCode());
			}

			StringBuffer sb = new StringBuffer();
			String tmp;

			while((tmp = br.readLine()) != null) {
				sb.append(tmp + "\n");
			}

			close();
			
			return sb.toString();
		}
	}

	public String request(String urlString, String method, String[] params) throws IOException, HttpConnectException {
		StringBuffer param = new StringBuffer();
		for(int i = 0 ; i < params.length ; i++) {
			if(i != 0) {
				param.append("&");
			}
			param.append(params[i]);
		}

		if(method.equals("GET")) {		
			url = new URL(urlString+"?"+param.toString());
			connection = url.openConnection();
			hURLc = (HttpURLConnection)connection;
			
			hURLc.setRequestMethod(method);
			hURLc.setDoOutput(true);
			hURLc.setDoInput(true);
			hURLc.setUseCaches(false);
			hURLc.setDefaultUseCaches(false);

			isr = new InputStreamReader(hURLc.getInputStream(), "UTF-8");
			br = new BufferedReader(isr);
			
			if(hURLc.getResponseCode() != HttpURLConnection.HTTP_OK) {
				throw new HttpConnectException(hURLc.getResponseCode());
			}

			StringBuffer sb = new StringBuffer();
			String tmp;

			while((tmp = br.readLine()) != null) {
				sb.append(tmp + "\n");
			}

			close();
			
			return sb.toString();
		} else {
			url = new URL(urlString);
			connection = url.openConnection();
			hURLc = (HttpURLConnection)connection;
			
			hURLc.setRequestMethod(method);
			hURLc.setDoOutput(true);
			hURLc.setDoInput(true);
			hURLc.setUseCaches(false);
			hURLc.setDefaultUseCaches(false);

			
			out = hURLc.getOutputStream();
			out.write(param.toString().getBytes("UTF-8"));
			out.flush();
			out.close();

			isr = new InputStreamReader(hURLc.getInputStream(), "UTF-8");
			br = new BufferedReader(isr);
			
			if(hURLc.getResponseCode() != HttpURLConnection.HTTP_OK) {
				throw new HttpConnectException(hURLc.getResponseCode());
			}

			StringBuffer sb = new StringBuffer();
			String tmp;

			while((tmp = br.readLine()) != null) {
				sb.append(tmp + "\n");
			}
			
			close();
			
			return sb.toString();
		}
	}

}
